FrameLib  2.0
DSP processing with frames of arbitrary timing and length
FrameLib_FixedPoint.h
Go to the documentation of this file.
1 
2 #ifndef FRAMELIB_FIXEDPOINT_H
3 #define FRAMELIB_FIXEDPOINT_H
4 
5 #include <stdint.h>
6 #include <cmath>
7 #include <limits>
8 
27 struct FL_SP
28 {
29  // Constructors
30 
31  FL_SP() {}
32  FL_SP(uint64_t a, uint64_t b, uint64_t c) : mInt(a), mFracHi(b), mFracLo(c) {}
33 
34  // Get Components
35 
36  uint64_t intVal() const { return mInt; }
37  uint64_t fracHiVal() const { return mFracHi; }
38  uint64_t fracLoVal() const { return mFracLo; }
39 
40  // Necessary Operations
41 
42  friend FL_SP qMul(const FL_SP& a, const uint64_t& intVal, const uint64_t& fracVal);
43  friend FL_SP operator * (const FL_SP& a, const FL_SP& b);
44  friend FL_SP twoMinus(const FL_SP& b);
45 
46 private:
47 
48  uint64_t mInt;
49  uint64_t mFracHi;
50  uint64_t mFracLo;
51 };
52 
53 
66 class FL_FP
67 {
68 
69 public:
70 
71  // Constructors
72 
73  FL_FP() : mInt(0U), mFrac(0U) {}
74  FL_FP(uint64_t a, uint64_t b) : mInt(a), mFrac(b) {}
75  FL_FP(const FL_SP& val);
76  FL_FP(const double& val);
77 
78  // Int and Fract
79 
80  uint64_t intVal() const { return mInt; }
81  uint64_t fracVal() const { return mFrac; }
82 
83  // Comparison operators (N.B. - it is faster to avoid branching using bit rather logical operators)
84 
85  friend bool operator == (const FL_FP& a, const FL_FP& b) { return (a.mInt == b.mInt & a.mFrac == b.mFrac); }
86  friend bool operator != (const FL_FP& a, const FL_FP& b) { return !(a == b); }
87 
88  friend bool operator < (const FL_FP& a, const FL_FP& b) { return ((a.mInt < b.mInt) | (a.mInt == b.mInt & a.mFrac < b.mFrac)); }
89  friend bool operator > (const FL_FP& a, const FL_FP& b) { return ((a.mInt > b.mInt) | (a.mInt == b.mInt & a.mFrac > b.mFrac)); }
90  friend bool operator <= (const FL_FP& a, const FL_FP& b) { return !(a > b); }
91  friend bool operator >= (const FL_FP& a, const FL_FP& b) { return !(a < b); }
92 
93  // Zero Test
94 
95  friend bool operator ! ( const FL_FP& b) { return !b.mInt && !b.mFrac; }
96 
97  // Arithmetic Operators
98 
99  friend FL_FP operator + (const FL_FP& a, const FL_FP& b);
100  friend FL_FP operator - (const FL_FP& a, const FL_FP& b);
101  friend FL_FP operator * (const FL_FP& a, const FL_FP& b);
102  friend FL_FP operator / (const FL_FP& a, const FL_FP& b);
103 
104  // Arithmetic Operators with Assignment
105 
106  FL_FP& operator += (const FL_FP& b)
107  {
108  *this = *this + b;
109  return *this;
110  }
111 
112  FL_FP& operator -= (const FL_FP& b)
113  {
114  *this = *this - b;
115  return *this;
116  }
117 
118  FL_FP& operator *= (const FL_FP& b)
119  {
120  *this = *this * b;
121  return *this;
122  }
123 
124  FL_FP& operator /= (const FL_FP& b)
125  {
126  *this = *this / b;
127  return *this;
128  }
129 
130  // Increment / Decrement Operatiors
131 
132  FL_FP& operator ++ ()
133  {
134  if (++mFrac == uint64_t(0U))
135  ++mInt;
136 
137  return *this;
138  }
139 
140  FL_FP operator ++ (int)
141  {
142  FL_FP result = *this;
143  operator++();
144  return result;
145  }
146 
147  FL_FP& operator -- ()
148  {
149  if (mFrac-- == uint64_t(0U))
150  --mInt;
151 
152  return *this;
153  }
154 
155  FL_FP& operator -- (int)
156  {
157  FL_FP& result = *this;
158  operator--();
159  return result;
160  }
161 
162  // Support for operators using doubles
163 
164  // Conversion
165 
166  operator double() const { return ((double) mInt) + ((double) mFrac / 18446744073709551616.0); }
167 
168  FL_FP& operator = (const double& a)
169  {
170  *this = FL_FP(a);
171  return *this;
172  }
173 
174  // Comparison
175 
176  friend bool operator == (const FL_FP& a, const double& b) { return a == FL_FP(b); }
177  friend bool operator == (const double& a, const FL_FP& b) { return FL_FP(a) == b; }
178  friend bool operator != (const FL_FP& a, const double& b) { return a != FL_FP(b); }
179  friend bool operator != (const double& a, const FL_FP& b) { return FL_FP(a) != b; }
180 
181  friend bool operator < (const FL_FP& a, const double& b) { return a < FL_FP(b); }
182  friend bool operator < (const double& a, const FL_FP& b) { return FL_FP(a) < b; }
183  friend bool operator > (const FL_FP& a, const double& b) { return a > FL_FP(b); }
184  friend bool operator > (const double& a, const FL_FP& b) { return FL_FP(a) > b; }
185  friend bool operator <= (const FL_FP& a, const double& b) { return a <= FL_FP(b); }
186  friend bool operator <= (const double& a, const FL_FP& b) { return FL_FP(a) <= b; }
187  friend bool operator >= (const FL_FP& a, const double& b) { return a >= FL_FP(b); }
188  friend bool operator >= (const double& a, const FL_FP& b) { return FL_FP(a) >= b; }
189 
190  // Arithmetic Operators
191 
192  friend FL_FP operator + (const FL_FP& a, const double& b) { return a + FL_FP(b); }
193  friend FL_FP operator + (const double& a, const FL_FP& b) { return FL_FP(a) + b; }
194  friend FL_FP operator - (const FL_FP& a, const double& b) { return a - FL_FP(b); }
195  friend FL_FP operator - (const double& a, const FL_FP& b) { return FL_FP(a) - b; }
196  friend FL_FP operator * (const FL_FP& a, const double& b) { return a * FL_FP(b); }
197  friend FL_FP operator * (const double& a, const FL_FP& b) { return FL_FP(a) * b; }
198  friend FL_FP operator / (const FL_FP& a, const double& b) { return a / FL_FP(b); }
199  friend FL_FP operator / (const double& a, const FL_FP& b) { return FL_FP(a) / b; }
200 
201  // Arithmetic Operators with Assignment
202 
203  FL_FP& operator += (const double& b) { return operator +=(FL_FP(b)); }
204  FL_FP& operator -= (const double& b) { return operator -=(FL_FP(b)); }
205  FL_FP& operator *= (const double& b) { return operator *=(FL_FP(b)); }
206  FL_FP& operator /= (const double& b) { return operator /=(FL_FP(b)); }
207 
208 private:
209 
210  uint64_t mInt;
211  uint64_t mFrac;
212 };
213 
214 #endif
uint64_t fracLoVal() const
Definition: FrameLib_FixedPoint.h:38
uint64_t fracHiVal() const
Definition: FrameLib_FixedPoint.h:37
FL_SP(uint64_t a, uint64_t b, uint64_t c)
Definition: FrameLib_FixedPoint.h:32
friend FL_SP operator*(const FL_SP &a, const FL_SP &b)
Definition: FrameLib_FixedPoint.cpp:126
FL_FP operator/(const FL_FP &a, const FL_FP &b)
Definition: FrameLib_FixedPoint.cpp:384
uint64_t fracVal() const
Definition: FrameLib_FixedPoint.h:81
high-precision unsigned fixed-point numerical format.
Definition: FrameLib_FixedPoint.h:66
FL_FP operator-(const FL_FP &a, const FL_FP &b)
Definition: FrameLib_FixedPoint.cpp:304
friend FL_SP twoMinus(const FL_SP &b)
Definition: FrameLib_FixedPoint.cpp:251
uint64_t intVal() const
Definition: FrameLib_FixedPoint.h:80
a minimal class for "super precision" fixed-point calculations where required.
Definition: FrameLib_FixedPoint.h:27
FL_SP()
Definition: FrameLib_FixedPoint.h:31
friend FL_SP qMul(const FL_SP &a, const uint64_t &intVal, const uint64_t &fracVal)
Definition: FrameLib_FixedPoint.cpp:31
uint64_t intVal() const
Definition: FrameLib_FixedPoint.h:36
FL_FP(uint64_t a, uint64_t b)
Definition: FrameLib_FixedPoint.h:74
FL_FP operator+(const FL_FP &a, const FL_FP &b)
Definition: FrameLib_FixedPoint.cpp:295
FL_FP()
Definition: FrameLib_FixedPoint.h:73